//  this library.
import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:encrypt/encrypt.dart' as Encrypt;
import 'package:encrypt/encrypt.dart';
import 'package:flutter/services.dart';
import 'package:pointycastle/asymmetric/api.dart';

/// 加密工具
class EncryptUtil {
  /// 计算验证码登录的key值
  static const String _key = "ce647efa-fc80-45a4-bec1-d5f9aa62d167";

  static const String _aesKey = "9b91f1963cf54dde82293db9295bb89b";

  static const String _gidAesKey = "05ee4559b3db2ab9a1f5d86a316c90d4";

  /// esa加密
  static Future<String> esaEncrypt(String data) async {
    String publicKeyStr = await rootBundle.loadString("key/esa_public.pem");
    final parser = RSAKeyParser();
    RSAPublicKey publicKey = parser.parse(publicKeyStr) as RSAPublicKey;
    var encrypter = Encrypter(RSA(publicKey: publicKey));
    var encrypt = encrypter.encrypt(data);
    return encrypt.base64;
  }

  // /// esa解密
  // static Future<String> esaDecrypt(String data) async {
  //   String publicKeyStr = await rootBundle.loadString("key/esa_private.pem");
  //   String publicKeyStr2 = await rootBundle.loadString("key/esa_public.pem");
  //   final parser = RSAKeyParser();
  //   RSAPrivateKey privateKey = parser.parse(publicKeyStr) as RSAPrivateKey;
  //   RSAPublicKey publicKey = parser.parse(publicKeyStr2) as RSAPublicKey;
  //   var encrypter = Encrypter(RSA(privateKey: privateKey));
  //   var decrypt64 = encrypter.decrypt(Encrypted.fromBase64(data));
  //   return decrypt64;
  // }

  /// md5加密，获取对应sign
  static String getMd5Sign({
    required String time,
    String phone = "",
  }) {
    // 根据时间戳 计算key值
    String md5key = _key + time.toString() + phone;
    var content = const Utf8Encoder().convert(md5key);
    var digest = md5.convert(content);
    return digest.toString();
  }

  /// aes 加密
  static String aesEncrypt({required String data}) {
    return _aesEncryptUtil(data: data, keyStr: _aesKey);
  }

  /// gid aes加密
  static String gidAesEncrypt({required String data}) {
    return _aesEncryptUtil(data: data, keyStr: _gidAesKey);
  }

  /// aes加密公用方法
  static String _aesEncryptUtil({
    required String data,
    required String keyStr,
  }) {
    //加密key
    var key = Encrypt.Key.fromUtf8(keyStr);
    //设置cbc模式
    var encrypter = Encrypt.Encrypter(Encrypt.AES(
      key,
      mode: Encrypt.AESMode.ecb,
    ));
    // 加密
    var encrypt = encrypter.encrypt(data);
    return encrypt.base64;
  }

  /// 解密答案方法
  static dynamic aesAnswer({
    required String userId,
    required String collectId,
    required String answerBase64,
  }) {
    String md5 = _getMd5Key(userId: userId, collectId: collectId);
    //解密key
    var key = Encrypt.Key.fromUtf8(md5);
    //设置cbc模式
    var encrypter = Encrypt.Encrypter(Encrypt.AES(
      key,
      mode: Encrypt.AESMode.ecb,
    ));
    // 解密
    String encrypt = encrypter.decrypt64(answerBase64);
    return encrypt;
  }

  /// md5加密
  static String _getMd5Key({
    required String userId,
    required String collectId,
  }) {
    var content = const Utf8Encoder().convert(userId + collectId);
    var digest = md5.convert(content);
    return digest.toString();
  }
}
